package com.competition.prediction.controller;

import com.competition.prediction.common.Result;
import com.competition.prediction.entity.Game;
import com.competition.prediction.service.GameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/game")
public class GameController {

    @Autowired
    private GameService gameService;

    @GetMapping("/list")
    public Result<List<Game>> getAllGames() {
        try {
            List<Game> games = gameService.getActiveGames();
            return Result.success(games);
        } catch (Exception e) {
            log.error("获取游戏列表失败", e);
            return Result.error("获取游戏列表失败");
        }
    }

    @GetMapping("/{id}")
    public Result<Game> getGameById(@PathVariable Long id) {
        try {
            Game game = gameService.getGameById(id);
            if (game != null) {
                return Result.success(game);
            }
            return Result.error("游戏不存在");
        } catch (Exception e) {
            log.error("获取游戏详情失败", e);
            return Result.error("获取游戏详情失败");
        }
    }
}